package com.otp.controller;

import com.otp.pojo.dto.CourseDto;
import com.otp.pojo.entity.Course;
import com.otp.pojo.request.course.*;
import com.otp.util.ResponseMessage;
import com.otp.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public ResponseMessage<List<CourseDto>> list() {
        return ResponseMessage.success(courseService.list()
                .stream()
                .map(Course::convert2Dto)
                .toList());
    }

    @GetMapping("/teacher/{teacherId}")
    public ResponseMessage<List<CourseDto>> listByTeacher(@PathVariable Long teacherId) {
        return ResponseMessage.success(courseService.listByTeacherId(teacherId)
                .stream()
                .map(Course::convert2Dto)
                .toList());
    }

    @GetMapping("/status/{status}")
    public ResponseMessage<List<CourseDto>> listByStatus(@PathVariable String status) {
        return ResponseMessage.success(courseService.listByStatus(status)
                .stream()
                .map(Course::convert2Dto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<CourseDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(courseService.getById(id)
                .convert2Dto());
    }

    @PostMapping
    public ResponseMessage<CourseDto> create(@RequestBody CourseCreateRequest request) {
        return ResponseMessage.success(courseService.create(request)
                .convert2Dto());
    }

    @PutMapping
    public ResponseMessage<CourseDto> update(@RequestBody CourseUpdateRequest request) {
        return ResponseMessage.success(courseService.update(request)
                .convert2Dto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        courseService.delete(id);
        return ResponseMessage.success(null);
    }
}